package com.wei.czz.framework.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.index.dao.UserLikeCommentDao;
import com.wei.czz.framework.index.entity.UserLikeCommentEntity;
import com.wei.czz.framework.index.service.UserLikeCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-09-26 22:15:00
 * className: UserLikeCommentServiceImpl 用户点赞博客评论操作请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("userLikeCommentService")
public class UserLikeCommentServiceImpl extends ServiceImpl<UserLikeCommentDao, UserLikeCommentEntity> implements UserLikeCommentService {

    private static final Logger log = LoggerFactory.getLogger(UserLikeCommentServiceImpl.class);

    @Override
    public void saveUserLikeComment(Long blogId, Long commentId) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaQueryWrapper<UserLikeCommentEntity> userLikeCommentLambdaWrapper = new LambdaQueryWrapper<>();
        // 查询 操作id和点赞状态 字段
        userLikeCommentLambdaWrapper.select(UserLikeCommentEntity::getOptId, UserLikeCommentEntity::getStatus);
        // 匹配 用户id、博客id和评论id 字段
        userLikeCommentLambdaWrapper.eq(UserLikeCommentEntity::getUserId, userPo.getUserId())
                .eq(UserLikeCommentEntity::getBlogId, blogId)
                .eq(UserLikeCommentEntity::getCommentId, commentId);
        // 查询数据库用户点赞博客评论表，匹配用户id、博客id、评论id关联的信息
        List<UserLikeCommentEntity> userLikeCommentList = baseMapper.selectList(userLikeCommentLambdaWrapper);
        if (userLikeCommentList.isEmpty()) {
            /*
                添加
             */
            UserLikeCommentEntity userLikeComment = new UserLikeCommentEntity();
            // 设置用户id、博客id、评论id、点赞状态（‘有效’）、更新时间和创建时间属性值
            userLikeComment.setUserId(userPo.getUserId())
                    .setBlogId(blogId)
                    .setCommentId(commentId)
                    .setStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setCreateTime(date);
            // 插入数据
            int count = baseMapper.insert(userLikeComment);
            log.info("保存用户点赞博客评论完成。count={}", count);
        } else {
            /*
                修改
             */
            if (userLikeCommentList.size() > 1) {
                log.warn("用户点赞博客评论存在重复数据，请务必及时处理。userId={} blogId={} commentId={}",
                        userPo.getUserId(), blogId, commentId);
            }
            UserLikeCommentEntity userLikeComment = userLikeCommentList.get(0);
            if (userLikeComment.getStatus().equals(CommonEnum.ZERO.getValue())) {
                log.info("用户重复点赞博客评论。status={}", userLikeComment.getStatus());
                throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "您已经点赞该博客评论，不可重复点赞。");
            }
            // 设置点赞状态为‘有效’
            UserLikeCommentEntity _userLikeComment = new UserLikeCommentEntity();
            _userLikeComment.setOptId(userLikeComment.getOptId())
                    .setStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date);
            int count = baseMapper.updateById(_userLikeComment);
            log.info("修改用户点赞博客评论完成。count={}", count);
        }
    }

    @Override
    public void userUnlikeComment(Long blogId, Long commentId) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaQueryWrapper<UserLikeCommentEntity> userLikeCommentLambdaWrapper = new LambdaQueryWrapper<>();
        // 查询 操作id和点赞状态 字段
        userLikeCommentLambdaWrapper.select(UserLikeCommentEntity::getOptId, UserLikeCommentEntity::getStatus);
        // 匹配 用户id、博客id和评论id 字段
        userLikeCommentLambdaWrapper.eq(UserLikeCommentEntity::getUserId, userPo.getUserId())
                .eq(UserLikeCommentEntity::getBlogId, blogId)
                .eq(UserLikeCommentEntity::getCommentId, commentId);
        // 查询数据库用户点赞博客评论表，匹配用户id、博客id、评论id关联的信息
        List<UserLikeCommentEntity> userLikeCommentList = baseMapper.selectList(userLikeCommentLambdaWrapper);
        if (userLikeCommentList.isEmpty()) {
            log.info("用户点赞过博客评论列表为空。userId={} blogId={} commentId={}", userPo.getUserId(), blogId, commentId);
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "您没有点赞过该评论，无法取消点赞。");
        }
        if (userLikeCommentList.size() > 1) {
            log.warn("用户点赞博客评论存在重复数据，请务必及时处理。userId={} blogId={} commentId={}", userPo.getUserId(),
                    blogId, commentId);
        }
        UserLikeCommentEntity userLikeComment = userLikeCommentList.get(0);

        if (userLikeComment.getStatus().equals(CommonEnum.ONE.getValue())) {
            log.info("用户没有点赞过博客评论。userId={} blogId={} commentId={}", userPo.getUserId(), blogId, commentId);
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "您没有点赞过该评论，无法取消点赞。");
        }

        // 设置点赞状态（‘无效’）和更新时间属性值
        UserLikeCommentEntity _userLikeComment = new UserLikeCommentEntity();
        _userLikeComment.setOptId(userLikeComment.getOptId())
                .setStatus(CommonEnum.ONE.getValue())
                .setUpdateTime(date);
        int count = baseMapper.updateById(_userLikeComment);
        log.info("用户取消点赞博客评论完成。count={}", count);
    }
}